package cglgu.trellis.chado.das2.server;

/*
 * $Id: ChadoSourcesPlugin.java 191 2011-03-27 00:38:22Z sundaram.georgetown@gmail.com $
 * $Author: sundaram.georgetown@gmail.com $
 * $Date: 2011-03-27 00:38:22 +0000 (Sun, 27 Mar 2011) $
 * $Rev: 191 $
 * $HeadURL: http://genomancer.googlecode.com/svn/branches/gu-dev-branch/tengcha/src/cglgu/trellis/chado/das2/server/ChadoSourcesPlugin.java $
 */

import genomancer.trellis.das2.model.Das2SourcesCapabilityI;
import genomancer.trellis.das2.server.ServerUtils;
import genomancer.trellis.das2.server.TrellisSourcesPluginI;
import cglgu.trellis.chado.das2.modelimpl.ChadoSourcesCapability;
import genomancer.vine.das2.client.modelimpl.Das2SourcesCapability;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ChadoSourcesPlugin implements TrellisSourcesPluginI  {
	static String VERSION = "0.5";  // current version of Das2ProxySourcesPlugin

	ChadoSourcesCapability sources_proxy = null;
	Map<String, String> params = null;

	public boolean init(ServletConfig config)  {
		return init( ServerUtils.getInitParams(config) );
	}

	public boolean init(Map<String, String> init_params)   {
		boolean success = false;
		try {
			params = init_params;
			String sources_query = params.get("sources_query");
			URI sources_query_uri = new URI(sources_query);
			URI base_uri = sources_query_uri.resolve("");
			System.out.println("sources_query_uri: " + sources_query_uri);
			System.out.println("base_uri: " + base_uri);
			sources_proxy = new ChadoSourcesCapability(base_uri, sources_query, params);
			success = true;
		} catch (URISyntaxException ex) {
			Logger.getLogger(ChadoSourcesPlugin.class.getName()).log(Level.SEVERE, null, ex);
		}
		return success;
	}

	public Das2SourcesCapabilityI getSourcesCapability()  { return sources_proxy; }

	public void addHeaders(HttpServletRequest request, HttpServletResponse response) {
		response.addHeader("DAS2-Server", "ChadoSourcesPlugin/" + VERSION);
	}

}